package cn.ecut.servlet;

import cn.ecut.model.Content;
import cn.ecut.services.ContentServices;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/contentList")
public class ContentListServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String num = request.getParameter("num");
        Integer begin = Integer.valueOf(num);

        ContentServices contentServices = new ContentServices();
        List<Content> list = contentServices.list(begin);

        Map<String , Object> map = new HashMap<>();
        String msg="";
        if (list.size()>0){
            msg="成功";
            map.put("msg",msg);
            map.put("status",0);
            map.put("data",list);
        }else {
            msg = "失败";
            map.put("msg",msg);
            map.put("status",-1);
            map.put("data",null);
        }

        response.getWriter().write(JSON.toJSONString(map));
    }
}
